const Router = require('koa-router');
const login = new Router();
const db = require('../utils/db.js');
const bodyParser = require('koa-bodyparser');
login.use(bodyParser());


//登录验证接口
login.post("/", async (ctx) => {
	// console.log(ctx.request.body)
	let username = ctx.request.body.username;
	let pwd = ctx.request.body.password;
  
	//判断数据库是否有这个账号
	let seachsql = `select * from user where username='${username}'`;
  
	let myarr = await new Promise((resolve, reject) => {
	  return db.query(seachsql, (err, data) => {
		if (err) throw err;
		resolve(data); //扔出去一个数组赋值给myarr
	  });
	});
	//判断搜索数组长度是否大于0
	if (myarr.length > 0) {
	  //有账号验证密码
	  // console.log(myarr);
	  if (myarr[0].password == pwd) {
		ctx.body = {
			code:"200",
			msg:"success"
		}
	  } else {
		ctx.body = {
			code:"404",
			msg:"error"
		};
	  }
	} else {
	  //无账号注册
	  ctx.body = {
		  code:"404",
		  msg:"没有账号"
	  };
	}
  });

module.exports = login;